package cl.foursoft.sirvicod.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import cl.foursoft.sirvicod.dao.facade.FacadePersona;
import cl.foursoft.sirvicod.dao.implementacion.DBMannager;
import cl.foursoft.sirvicod.dao.transferObject.PersonaTO;
import cl.foursoft.sirvicod.util.UtilLog;

public class PersonaService {
	
	public PersonaTO obtenerPersonaPorRut(String rut){
		
		Connection c = DBMannager.getConnection();
		
		FacadePersona facadePersona = new FacadePersona();
		
		PersonaTO resp = null;
		
		try {
			
			resp = facadePersona.obtenerPersonaPorRut(rut, c);
			
			boolean exito = (resp != null);
			
			DBMannager.close(c, exito);
			
			return resp;
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
			return resp;
		}
	}
	
	public PersonaTO obtenerPersonaFullPorRut(String rut){
		
		Connection c = DBMannager.getConnection();
		FacadePersona facadePersona = new FacadePersona();		
		PersonaTO resp = null;
		
		try {
			
			resp = facadePersona.obtenerPersonaFullPorRut(rut, c);
			
			boolean exito = (resp != null);			
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		return resp;
	}
	
	public List<PersonaTO> obtenerPersonasAutorizadasPorDepto(int depto){
		
		Connection c = DBMannager.getConnection();
		FacadePersona facadePersona = new FacadePersona();
		List<PersonaTO> resp = new ArrayList<PersonaTO>();
		
		try{
			
			resp = facadePersona.obtenerPersonasAutorizadasPorDepto(depto, c);
			
			boolean exito = (resp != null);
			DBMannager.close(c, exito);
		}
		catch (Exception ex) 
		{
			UtilLog.registrar(ex);
			DBMannager.closeOnError(c);
		}
		return resp;
	}

}
